package com.example.myproject.mapper;

import com.example.myproject.model.UserRatioDetail;
import org.apache.ibatis.annotations.*;

@Mapper
public interface UserRatioDetailMapper {

    @Select("SELECT * FROM user_ratio_detail WHERE user_id = #{userId} AND infohash = #{infohash}")
    UserRatioDetail findByUserIdAndInfohash(@Param("userId") Long userId, @Param("infohash") String infohash);

    @Insert("INSERT INTO user_ratio_detail(user_id, upload, download, infohash) " +
            "VALUES(#{userId}, #{upload}, #{download}, #{infohash})")
    @Options(useGeneratedKeys = true, keyProperty = "ratioDetailId")
    int insert(UserRatioDetail detail);

    @Update("UPDATE user_ratio_detail SET upload = #{upload}, download = #{download} " +
            "WHERE user_id = #{userId} AND infohash = #{infohash}")
    int update(UserRatioDetail detail);

    @Select("SELECT SUM(upload) FROM user_ratio_detail WHERE user_id = #{userId}")
    Long sumUploadByUserId(@Param("userId") Long userId);

    @Select("SELECT SUM(download) FROM user_ratio_detail WHERE user_id = #{userId}")
    Long sumDownloadByUserId(@Param("userId") Long userId);
}
